package zengjin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zengjin.common.R;
import zengjin.model.dto.admin.UpdateAdminPersonalDto;
import zengjin.model.en.Admin;
import zengjin.model.en.Image;
import zengjin.model.vo.admin.*;
import zengjin.service.AdminService;
import zengjin.service.ImageService;

/**
 * @author 19324
 */
@Tag(name = "管理员相关接口")
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private ImageService imageService;


    //1.管理员个人信息
    @Operation(summary = "管理员个人信息")
    @PostMapping("/queryAdminPersonal")
    public R<AdminPersonalVO> queryAdminPersonal(Long adminId) {
        //1.参数校验
        if (adminId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Admin admin = adminService.getById(adminId);
        AdminPersonalVO adminPersonalVO = BeanUtil.copyProperties(admin, AdminPersonalVO.class);
        //3.查询头像
        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("image_name", "admin_avatar");
        imageQueryWrapper.eq("image_name_id", adminId);
        Image image = imageService.getOne(imageQueryWrapper);
        if (image != null) {
            adminPersonalVO.setUserAvatarUrl(image.getImageUrl());
        }
        return R.success(adminPersonalVO, "查询成功");
    }

    //2.修改管理员个人信息
    @Operation(summary = "修改雇主个人信息")
    @PostMapping("/updateAdminPersonal")
    public R<Boolean> updateAdminPersonal(@RequestBody UpdateAdminPersonalDto adminPersonalDto) {
        //1.参数校验
        if (adminPersonalDto == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean result = adminService.updateAdminPersonal(adminPersonalDto);
        if (!result) {
            throw new RuntimeException("修改失败");
        }
        return R.success(true, "修改成功");
    }

    //3.查询出管理员-首页数据总汇
    @Operation(summary = "查询出管理员-首页数据总汇")
    @PostMapping("/queryAdminData")
    public R<AdminDateVO> queryAdminData() {
        AdminDateVO adminDateVO = adminService.queryAdminData();
        return R.success(adminDateVO, "查询成功");
    }

    //4.查询出管理员-首页企业行业情况
    @Operation(summary = "查询出管理员-首页企业行业情况")
    @PostMapping("/queryAdminEmployerData")
    public R<AdminEmployerDataVO> queryAdminEmployerData() {
        AdminEmployerDataVO adminEmployerDataVO = adminService.queryAdminEmployerData();
        return R.success(adminEmployerDataVO, "查询成功");

    }

    //5.查询出管理员-首页求职者学历分布
    @Operation(summary = "查询出管理员-首页求职者学历分布")
    @PostMapping("/queryAdminEducationData")
    public R<AdminEducationDataVO> queryAdminEducationData() {
     AdminEducationDataVO adminEducationDataVO = adminService.queryAdminEducationData();
     return R.success(adminEducationDataVO, "查询成功");
    }

    //6.查询出管理员-首页在招岗位变化
    @Operation(summary = "查询出管理员-首页在招岗位变化")
    @PostMapping("/queryAdminJobData")
    public R<AdminJobDataVO> queryAdminJobData() {
        AdminJobDataVO adminJobDataVO = adminService.queryAdminJobData();
        return R.success(adminJobDataVO, "查询成功");
    }

}
